/* Note: 	contract_reg_final.do runs contract-level regressions.

*  Input: 	fxd_contract_reg_final.dta		

*  Tables:  fxd_contracts.tex					// Table 3
			supply_shock_A_fs_binding.tex		// Table 6

*/

clear all

grstyle init
grstyle set plain, horizontal grid
grstyle set color Set1

********************************************************************************
*	Contract-level summary stat 
********************************************************************************
 
use "./Data/fxd_contract_reg_final", clear

// Note: Contract controls FXD_* are frozen as of 2009. 

keep if Year==2009
drop NonKRW_WT
g BankNotionalNet_USDm= BankNotionalNet_USDk/1000
lab var BankNotionalNet_USDm "Notional Net (USD mio)"
g BankNotionalNetA_pct= BankNotionalNetA*100

foreach char in "sell_WT" "USDKRW_WT" "JPYKRW_WT" "EURKRW_WT" "FXFwd_WT" "FXSwap_WT" "FXOpt_WT" "FXFut_WT"{
g `char'_pctg= `char'*100
}

label var sell_WT_pctg "Direction: Firm sells FC (\%)"
label var USDKRW_WT_pctg "Pair: USD-KRW (\%)"
label var JPYKRW_WT_pctg "Pair: JPY-KRW (\%)"
label var EURKRW_WT_pctg "Pair: EUR-KRW (\%)"
label var FXFwd_WT_pctg "Type: Forwards (\%)"
label var FXSwap_WT_pctg "Type: Swaps (\%)"
label var FXOpt_WT_pctg "Type: Options (\%)"
label var FXFut_WT_pctg "Type: Futures (\%)"
label var BankNotionalNetA_pct "FXDNet/Assets (\%)"
eststo full: quietly estpost summarize BankNotionalNet_USDm BankNotionalNetA_pct *_pctg if binding==0 | binding==1
eststo exposed: quietly estpost summarize BankNotionalNet_USDm BankNotionalNetA_pct *_pctg if binding==1
eststo nonexposed: quietly estpost summarize BankNotionalNet_USDm BankNotionalNetA_pct *_pctg if binding==0
eststo diff: quietly estpost ttest BankNotionalNet_USDm BankNotionalNetA_pct *_pctg, by(binding) unequal
esttab exposed nonexposed diff, mtitles("Exposed" "Non-Exposed" "Diff") ///
cells("mean(pattern(1 1 0) fmt(3)) b(star pattern(0 0 1) fmt(2)) t(pattern(0 0 1) par fmt(2))") label

esttab full exposed nonexposed diff, mtitles("Full Sample" "Constrained" "Unconstrained" "Difference") cells("mean(pattern(1 1 1 0) fmt(%12.1fc)) sd(pattern(1 1 1 0) fmt(%12.0fc)) b(star pattern(0 0 0 1) fmt(%12.0fc)) t(pattern(0 0 0 1) par fmt(1))") replace nonumber label
 
local fn="./Output/Table_3_fxd_contracts.tex"
esttab full exposed nonexposed diff using `fn', mtitles("Full Sample" "Constrained" "Unconstrained" "Difference") cells("mean(pattern(1 1 1 0) fmt(%12.1fc)) sd(pattern(1 1 1 0) fmt(%12.0fc)) b(star pattern(0 0 0 1) fmt(%12.0fc)) t(pattern(0 0 0 1) par fmt(1))") replace nonumber label


 


********************************************************************************
*	Contract-level regression (Table 6)
********************************************************************************
 
foreach tag in "A"{  
	local yvar="dBankNotionalNet"+"`tag'"
	display "`yvar'"
	foreach xvar in "binding" {  
		foreach tag2 in "fs" { 
			use "./Data/fxd_contract_reg_final", clear


			reg `yvar' `xvar' if Year==2010, vce(cluster bank)
			eststo m1
			estadd local FirmFE "N"
			estadd local ContractFE "N"
			estadd local BankFE "N"


			reg `yvar' `xvar' ///
			Bank_size Bank_lta Bank_leverage FB ///
			bank_weight FXD_FXSwap FXD_FXOpt FXD_FXFut FXD_EUR FXD_JPY FXD_XXX ///
			Firm_size Firm_derivS Firm_fcl i.Industry1 ///
			if Year==2010, vce(cluster bank)
			eststo m8
			estadd local FirmFE "Y"
			estadd local ContractFE "Y"
			estadd local BankFE "Y"



			/* FXD net sellers only */
			use "./Data/fxd_contract_reg_final", clear
			keep if FXD_netsell==1
			reg `yvar' `xvar' if Year==2010, vce(cluster bank)
			eststo m11
			estadd local FirmFE "N"
			estadd local ContractFE "N"
			estadd local BankFE "N"

			
			reg `yvar' `xvar' ///
			Bank_size Bank_lta Bank_leverage FB ///
			bank_weight FXD_FXSwap FXD_FXOpt FXD_FXFut FXD_EUR FXD_JPY FXD_XXX ///
			Firm_size Firm_derivS Firm_fcl i.Industry1 ///
			if Year==2010, vce(cluster bank)
			eststo m18
			estadd local FirmFE "Y"
			estadd local ContractFE "Y"
			estadd local BankFE "Y"

			
			
			/* FXD Net buyers only */
			use "./Data/fxd_contract_reg_final", clear
			keep if FXD_netsell==0
			reg `yvar' `xvar' if Year==2010, vce(cluster bank)
			eststo m21
			estadd local FirmFE "N"
			estadd local ContractFE "N"
			estadd local BankFE "N"

			reg `yvar' `xvar' ///
			Bank_size Bank_lta Bank_leverage FB ///
			bank_weight FXD_FXSwap FXD_FXOpt FXD_FXFut FXD_EUR FXD_JPY FXD_XXX ///
			Firm_size Firm_derivS Firm_fcl i.Industry1 ///
			if Year==2010, vce(cluster bank)
			eststo m28
			estadd local FirmFE "Y"
			estadd local ContractFE "Y"
			estadd local BankFE "Y"
				 
			
			esttab m11 m18 m21 m28 m1 m8, s(FirmFE BankFE N r2, /// 
			label("FirmControls" "BankControls" "N" "RSqr")) ar(2) star(* 0.1 ** 0.05 *** 0.01 ) ///
			mtitle("Exporters" "Exporters" "Non-exporters" "Non-exporters" "Full Sample" "Full Sample") ///
			keep(`xvar' FXD*) label replace	
				 
			local fn="./Output/Table_6_supply_shock_`tag'_`tag2'_`xvar'.tex"
			 
			esttab m11 m18 m21 m28 m1 m8 using `fn', s(FirmFE BankFE N r2, /// 
			label("FirmControls" "BankControls" "N" "RSqr")) ar(2) star(* 0.1 ** 0.05 *** 0.01 ) ///
			mtitle("Exporters" "Exporters" "Non-exporters" "Non-exporters" "Full Sample" "Full Sample") ///
			keep(`xvar' FXD*) label replace			
		}
	} 
}

